package com.demo.test.flyweight;

import com.demo.test.flyweight.iml.Flyweight;

import java.util.HashMap;

public class FlyweightFactory {

    private HashMap<String,Flyweight> flyweights = new HashMap<String,Flyweight>();

    public Flyweight getFlyweight(String key){
        Flyweight flyweight = (Flyweight) flyweights.get(key);

        if(flyweight != null){
            System.out.println("具体享元"+key+"已经存在，被成功获取");
        }else{

            if(key.equals("a")){
                flyweight = new ConcreteFlyweightA(key);
            }else if(key.equals("b")){
                flyweight = new ConcreteFlyweightB(key);
            }else{
                return null;
            }

            flyweights.put(key,flyweight);
        }
        System.out.println("实例数量："+flyweights.size());
        return flyweight;
    }
}
